<!-- 分栏布局 -->
<script lang="ts" setup>
import ToolBarLeft from '@/layouts/components/Hheader/ToolBarLeft.vue'
import ToolBarRight from '@/layouts/components/Hheader/ToolBarRight.vue'
import SubMenu from '@/layouts/components/Menu/SubMenu.vue'
import Main from '@/layouts/components/Main/index.vue'
import {computed, ref, watch} from 'vue'
import {useRoute, useRouter} from 'vue-router'
import useRouteStore from '@/store/route.ts'
import useThemeStore from '@/store/theme.ts'

const route = useRoute()
const router = useRouter()
const routerStore = useRouteStore()
const themeStore = useThemeStore()
const subMenu = ref<any[]>([])
const splitActive = ref<string>('')
const menuList = computed(() => routerStore.getShowMenuList)
const activeMenu = computed(() => (route.meta.activeMenu ? route.meta.activeMenu : route.path))
const isCollapse = computed(() => themeStore.isCollapse)
const baseTitle = import.meta.env.VITE_APP_TITLE

watch(
	() => [menuList, route],
	() => {
		// 当前菜单没有数据直接 return
		if (!menuList.value.length) return
		splitActive.value = route.path
		const menuItem = menuList.value.filter(
			(item: any) => route.path === item.path || `/${route.path.split('/')[1]}` === item.path
		)
		if (menuItem[0].children?.length) return (subMenu.value = menuItem[0].children)
		subMenu.value = []
	},
	{
		deep: true,
		immediate: true
	}
)

const changeSubMenu = (item: any) => {
	splitActive.value = item.path
	if (item.children?.length) return (subMenu.value = item.children)
	subMenu.value = []
	void router.push({path: item.path})
}
</script>

<template>
	<el-container class="layout">
		<div class="aside-split">
			<div class="logo flx-center">
				<img alt="logo" src="@/assets/images/logo.svg">
			</div>
			<el-scrollbar>
				<div class="split-list">
					<div
						v-for="item in menuList"
						:key="item.path"
						:class="{ 'split-active': splitActive === item.path || `/${splitActive.split('/')[1]}` === item.path }"
						class="split-item"
						@click="changeSubMenu(item)"
					>
						<el-icon>
							<component :is="item.meta.icon"/>
						</el-icon>
						<span class="title">{{ item.meta.title }}</span>
					</div>
				</div>
			</el-scrollbar>
		</div>
		<el-aside :class="{ 'not-aside': !subMenu.length }" :style="{ width: isCollapse ? '65px' : '210px' }">
			<div class="logo flx-center">
				<span v-show="subMenu.length">{{ isCollapse ? baseTitle.charAt(0) : baseTitle }}</span>
			</div>
			<el-scrollbar>
				<el-menu
					:collapse="isCollapse"
					:collapse-transition="false"
					:default-active="activeMenu"
					:router="false"
					:unique-opened="true"
					background-color="#ffffff"
				>
					<SubMenu :menu-list="subMenu"/>
				</el-menu>
			</el-scrollbar>
		</el-aside>
		<el-container>
			<el-header>
				<ToolBarLeft/>
				<ToolBarRight/>
			</el-header>
			<Main/>
		</el-container>
	</el-container>
</template>

<style lang="scss" scoped>
@import "./index.scss";
</style>

<style lang="scss">
.columns {
	.el-menu,
	.el-menu--popup {
		.el-menu-item {
			&.is-active {
				background: var(--el-color-primary-light-9);

				&::before {
					position: absolute;
					top: 0;
					right: 0;
					bottom: 0;
					width: 4px;
					content: "";
					background: var(--el-color-primary);
				}
			}
		}
	}
}
</style>
